package com.qianfeng.chainmobile.api.controller.advice;

import com.qianfeng.chainmobile.api.exceptions.MyExceptionWithData;
import com.qianfeng.smsplatform.common.constants.InterfaceExceptionDict;
import com.qianfeng.smsplatform.common.exceptions.MyBaseException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
@ResponseBody
public class MyErrorController {
    @ExceptionHandler(Exception.class)
    public Map processException(Exception e) {
        e.printStackTrace();
        Map map = new HashMap();
        map.put("code", InterfaceExceptionDict.RETURN_STATUS_ERROR);
        map.put("msg","网络异常");
        return map;
    }

    @ExceptionHandler(MyBaseException.class)
    public Map processException(MyBaseException e) {
        e.printStackTrace();
        Map map = new HashMap();
        map.put("code", e.getCode());
        map.put("msg",e.getMessage());
        if (e instanceof MyExceptionWithData){ //判断是否为同一构造器
            map.put("data",((MyExceptionWithData) e).getData());
        }
        return map;
    }
}
